﻿using DoNetDrive.Core.Command;
using Face8280WebSocketServer.Command;
using Face8280WebSocketServer.Command.Models;
using Face8280WebSocketServer.Command.Record;
using Face8280WebSocketServer.Command.Remote;
using Face8280WebSocketServer.Controllers;
using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FaceFace8280WebSocketServer.Controllers.New.FaceDevice
{
    [Route("api/[controller]")]
    [ApiController]
    public class DeviceRemoteNewController : ControllerBase
    {
        ICacheService _CacheService;
        IConnectIOLogService _ConnectIOLogService;
        IFaceIOLogService _FaceIOLogService;
        public DeviceRemoteNewController(ICacheService cacheService, IConnectIOLogService connectIOLogService, IFaceIOLogService faceIOLogService)
        {
            _CacheService = cacheService;
            _ConnectIOLogService = connectIOLogService;
            _FaceIOLogService = faceIOLogService;
        }

        [HttpPost]
        [Route("Add")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public async Task<IActionResult> Add([FromForm] DeviceRemoteNewAddParameter par)
        {

            if (par.DeviceIDs == null)
            {
                return new JsonResult(new JsonResultModel(2, "未选择需要操作的设备"));
            }
            if (par.DeviceIDs.Count == 0)
            {
                return new JsonResult(new JsonResultModel(2, "未选择需要操作的设备"));
            }

            try
            {
                var method = this.GetType().GetMethod(par.Remote);
                var obj = method.Invoke(this, new object[] { par.DeviceIDs }) as Task<List<JsonResultModel>>;
                var result = await obj;
                return new JsonResult(result);
            }
            catch (Exception ex)
            {
                return new JsonResult(new JsonResultModel(100, ex.Message));
            }
        }
        /// <summary>
        /// 远程开锁
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftOpenRelay(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftOpenRelay cmd = new SoftOpenRelay(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 远程关锁
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftCloseRelay(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {

                SoftCloseRelay cmd = new SoftCloseRelay(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }

            return result;
        }


        /// <summary>
        /// 远程常开
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftKeepOpenRelay(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {

                SoftKeepOpenRelay cmd = new SoftKeepOpenRelay(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;

        }
        /// <summary>
        /// 远程锁定
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftLock(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftLock cmd = new SoftLock(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 远程解除锁定
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftUnlock(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftUnlock cmd = new SoftUnlock(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 解除报警
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftCloseAlarm(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftCloseAlarm cmd = new SoftCloseAlarm(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 触发消防报警
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftFireAlarm(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftFireAlarm cmd = new SoftFireAlarm(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 同步时间
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SetTime(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SetTime cmd = new SetTime(await GetCommandDetail(item), DateTime.Now, "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 重启设备
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftRestart(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftRestart cmd = new SoftRestart(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 恢复出厂设置
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> SoftInitialize(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                SoftInitialize cmd = new SoftInitialize(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 重新连接服务器
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> ReconnectServer(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                ReconnectServer cmd = new ReconnectServer(await GetCommandDetail(item), "user");
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <param name="sn"></param>
        public async Task<List<JsonResultModel>> GetStatus(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                GetStatus cmd = new GetStatus(await GetCommandDetail(item));
                await Allocator.AddCommandAsync(cmd);
                var cmdResult = cmd.getResult() as CommandResult;
                var resultModel = new JsonResultModel(sn);
                result.Add(resultModel);
            }
            return result;
        }
        /// <summary>
        /// 重置出入记录
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public async Task<List<JsonResultModel>> RereadRecord(List<string> sn)
        {
            var result = new List<JsonResultModel>();
            foreach (var item in sn)
            {
                RereadRecord cmd = new RereadRecord(await GetCommandDetail(item), RecordType.Enter);
                var cmdResult = await ExecuteCommand(cmd, item);
                result.Add(cmdResult);
            }
            return result;
        }
        private async Task<INCommandDetail> GetCommandDetail(string sn)
        {
            var first = Allocator.WebSocketClients[sn];
            var detail = await Allocator.GetCommandDetail(first);
            return detail;
        }

        private async Task<JsonResultModel> ExecuteCommand(JsonCommand cmd, string sn)
        {
            var result = new JsonResultModel();
            result.content = sn;
            try
            {
                var devcie = _CacheService.GetDevice(sn);
                await Allocator.AddCommandAsync(cmd);
                var response = cmd.getResult() as CommandResult;
                if (response == null || response.ResultObj.Result != 200)
                {
                    result.errCode = response.ResultObj.Result;
                    result.error = response.ResultObj.Error;
                    result.result = false;
                }
            }
            catch (Exception ex)
            {
                result.result = false;
                result.error = ex.Message;
                result.errCode = 100;
            }
            return result;
        }
    }
}
